package tech.youai.ppping;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.Map;

public class AlertManager {
    private final Map<String,String> urls;
    private final ThreadPoolTaskExecutor taskExecutor;
    private final UrlPing urlPing;
    private final Weixin weixin;
    private boolean running;

    public AlertManager(Map<String,String> urls, Weixin weixin) {
        this.urls = urls;
        this.weixin = weixin;
        taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setMaxPoolSize(20);
        urlPing = new UrlPing();
    }

    public void run(){
        this.running = true;
        taskExecutor.initialize();
        for (Map.Entry<String,String> urlMap : this.urls.entrySet()){
            runPingTask(urlMap.getKey(), urlMap.getValue());
        }
    }

    private void runPingTask(String name, String url) {
        AlertRunner alertRunner = new AlertRunner(urlPing, weixin, name, url);
        alertRunner.start();
    }


}
